package com.ruoyi.lvlian.service.impl;

import com.ruoyi.lvlian.controller.DriverInfoController;
import com.ruoyi.lvlian.domain.chart.*;
import com.ruoyi.lvlian.dto.ChartDataDto;
import com.ruoyi.lvlian.mapper.DashBoardMapper;
import com.ruoyi.lvlian.service.IDashBoardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DashBoardServiceImpl implements IDashBoardService {
    private static final Logger log = LoggerFactory.getLogger(DriverInfoController.class);
    @Autowired
    private DashBoardMapper dashBoardMapper;

    @Override

    public ChartData queryInvoiceInfoTotalAmountBySellerName(ChartDataDto dto) {
        // 获取原始数据
        List<Map<String, Object>> rawData = dashBoardMapper.queryInvoiceInfoTotalAmountBySellerName(dto);
        // 调用公共方法进行组装
        return buildChartData(rawData);
    }


    @Override
    public ChartData queryInvoiceInfoCntBySellerName(ChartDataDto dto) {
        List<Map<String, Object>> rawData = dashBoardMapper.queryInvoiceInfoCntBySellerName(dto);
        // 调用公共方法进行分组
        // 调用公共方法进行组装
        return buildChartData(rawData);
    }

    @Override
    public ChartData queryPaymentInfoAmountBySellerName(ChartDataDto dto) {
        List<Map<String, Object>> rawData = dashBoardMapper.queryPaymentInfoAmountBySellerName(dto);
        return buildChartData(rawData);
    }

    @Override
    public  List<PlaneData>   queryPaymentInfoAmountBySellerNameTotal(ChartDataDto dto) {
        List<PlaneData>  rawData = dashBoardMapper.queryPaymentInfoAmountBySellerNameTotal(dto);

        return rawData;
    }

    @Override
    public List<PlaneData> queryPaymentInfoCntBySellerName(ChartDataDto dto) {
        List<PlaneData>  rawData = dashBoardMapper.queryPaymentInfoCntBySellerName(dto);

        return rawData;
    }

    @Override
    public ChartData queryPaymentInfoCntBySellerName3d(ChartDataDto dto) {
        List<Map<String, Object>> rawData = dashBoardMapper.queryPaymentInfoCntBySellerName3d(dto);
        return buildChartData(rawData);
    }

    @Override
    public List<PlaneData> queryInvoiceInfoCntBySellerName2D(ChartDataDto dto) {
        List<PlaneData>  rawData = dashBoardMapper.queryInvoiceInfoCntBySellerName2D(dto);
        return rawData;
    }

    @Override
    public ChartData queryContractInfoByContractType3d(ChartDataDto dto) {
        List<Map<String, Object>> rawData = dashBoardMapper.queryContractInfoByContractType3d(dto);
        return buildChartData(rawData);
    }

    @Override
    public List<PlaneData> queryContractInfoByContractType2d(ChartDataDto dto) {
        List<PlaneData>  rawData = dashBoardMapper.queryContractInfoByContractType2d(dto);
        return rawData;
    }

    @Override
    public List<PlaneData> queryVehicleInfoCntBySellerName2d(ChartDataDto dto) {
        return  dashBoardMapper.queryVehicleInfoCntBySellerName2d(dto);
    }

    @Override
    public ChartData queryVehicleInfoCntBySellerName3d(ChartDataDto dto) {
        List<Map<String, Object>> rawData = dashBoardMapper.queryVehicleInfoCntBySellerName3d(dto);
        return buildChartData(rawData);
    }

    @Override
    public List<PlaneData> queryInvoiceInfoTotalAmountBySellerName2d(ChartDataDto dto) {
        return  dashBoardMapper.queryInvoiceInfoTotalAmountBySellerName2d(dto);
    }

    @Override
    public ChartData queryWarehouseInfoSellerSettlementAmountBySellerName3d(ChartDataDto dto) {
        List<Map<String, Object>> rawData = dashBoardMapper.queryWarehouseInfoSellerSettlementAmountBySellerName3d(dto);
        return buildChartData(rawData);
    }

    @Override
    public ChartData queryBusinessLicenseInfoByType3d(ChartDataDto dto) {
        List<Map<String, Object>> rawData = dashBoardMapper.queryBusinessLicenseInfoByType3d(dto);
        return buildChartData(rawData);
    }

    @Override
    public List<PlaneData> queryBusinessLicenseInfoByType2d(ChartDataDto dto) {
        return  dashBoardMapper.queryBusinessLicenseInfoByType2d(dto);
    }

    @Override
    public List<PlaneData> queryDriverInfoCnt2d(ChartDataDto dto) {
        return  dashBoardMapper.queryDriverInfoCnt2d(dto);
    }

    @Override
    public ChartData queryDriverInfoCnt3d(ChartDataDto dto) {
        List<Map<String, Object>> rawData = dashBoardMapper.queryDriverInfoCnt3d(dto);
        return buildChartData(rawData);
    }

    @Override
    public List<PlaneData> queryWarehouseInfoSellerCnt2d(ChartDataDto dto) {
        return  dashBoardMapper.queryWarehouseInfoSellerCnt2d(dto);
    }

    @Override
    public ChartData queryWarehouseInfoSellerCnt3d(ChartDataDto dto) {
        List<Map<String, Object>> rawData = dashBoardMapper.queryWarehouseInfoSellerCnt3d(dto);
        return buildChartData(rawData);
    }

    @Override
    public List<PlaneData> queryWarehouseInfoSellerSettlementAmountBySellerName2d(ChartDataDto dto) {
        return  dashBoardMapper.queryWarehouseInfoSellerSettlementAmountBySellerName2d(dto);
    }


    /**
     * 公共方法，用于组装ChartData
     * @param rawData 查询结果的原始数据
     * @return 组装好的ChartData
     */
    public ChartData buildChartData(List<Map<String, Object>> rawData) {
        // 初始化ChartDataResponse
        ChartData chartDataResponse = new ChartData();
        Legend legend = new Legend();
        XAxis xAxis = new XAxis();
        List<Series> seriesList = new ArrayList<>();

        // 提取所有xAxis、legend和series数据
        Set<String> xAxisSet = new TreeSet<>(); // 使用Set保证xAxis唯一
        Map<String, Map<String, Double>> seriesDataMap = new HashMap<>();

        // 收集原始数据并构建映射表
        for (Map<String, Object> row : rawData) {
            String xdata = (String) row.get("xAxis");
            String sellerName = (String) row.get("legend");
            Double totalAmount = 0.0;
            if(row.get("seriesdata") instanceof Long  ){
                  totalAmount =  ((Long) row.get("seriesdata")).doubleValue() ;
            }else {
                totalAmount = (Double) row.get("seriesdata");
            }

            // 收集xAxis
            xAxisSet.add(xdata);

            // 初始化卖家的数据映射
            seriesDataMap.computeIfAbsent(sellerName, k -> new HashMap<>()).put(xdata, totalAmount);
        }

        // 设置xAxis数据
        List<String> xAxisList = new ArrayList<>(xAxisSet);
        xAxis.setData(xAxisList);

        // 设置series数据并补全0
        for (Map.Entry<String, Map<String, Double>> entry : seriesDataMap.entrySet()) {
            String sellerName = entry.getKey();
            Map<String, Double> monthlyData = entry.getValue();

            Series series = new Series();
            series.setName(sellerName);

            // 对应每个xAxis补全数据
            List<Double> seriesData = new ArrayList<>();
            for (String month : xAxisList) {
                seriesData.add(monthlyData.getOrDefault(month, 0.0)); // 如果没有数据则补0
            }
            series.setData(seriesData);

            seriesList.add(series);
        }

        // 设置Legend数据
        legend.setData(seriesList.stream().map(Series::getName).collect(Collectors.toList()));

        // 组装返回结果
        chartDataResponse.setLegend(legend);
        chartDataResponse.setxAxis(xAxis);
        chartDataResponse.setSeries(seriesList);

        return chartDataResponse;
    }
}
